package com.accesshollywood.grfxOrdering.orderTreeSpecial;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.SQLException;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;

import com.accesshollywood.SQL.GFXSQL;
import com.accesshollywood.grfxOrdering.basics.Colors;
import com.accesshollywood.grfxOrdering.basics.Users;
import com.accesshollywood.grfxOrdering.orderTreeSpecial.models.ApprTableModel;
import com.accesshollywood.grfxOrdering.orderTreeSpecial.nodes.ApprTreeNode;
import com.accesshollywood.indexables.GrfxOrder;

public class ApprPanel extends JPanel {
	/*
	ApprTableModel model;
	public static final boolean VERBOSE = true;
	public static final boolean NOT_VERBOSE = false;

	public static final String[] verbose = { "Research", "PhotoEdit", "Footage", "Legal" };
	public static final String[] not_verbose = { "R", "Ph", "F", "L" };

	public static final int flagResearch = 1;// 2^0 000
	public static final int flagPhotoEdit = 2;// 2^1 001
	public static final int flagFootage = 4;// 2^2 010
	public static final int flagLegal = 8;// 2^3 100

	JPanel checkPanel;
	public JCheckBox checkResearch;
	public JCheckBox checkPhotoEdit;
	public JCheckBox checkFootage;
	public JCheckBox checkLegal;
	JPanel grabClosePanel;
	public JButton grabOrder;
	public JButton completeOrder;
	JCheckBox closeOrder;
	JCheckBox archiveOrder;

	public ApprPanel(final ApprTableModel model, int apprBitMask, int needsBitMask, boolean isVerbose) {
		this.model = model;

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		setBorder(new TitledBorder(new EtchedBorder(), ""));

		checkPanel = new JPanel();
		FlowLayout newFlow = new FlowLayout();
		newFlow.setHgap(0);
		newFlow.setVgap(0);
		// checkPanel.setLayout(newFlow);
		checkPanel.setLayout(new BoxLayout(checkPanel, BoxLayout.X_AXIS));
		add(checkPanel);

		String[] labels;

		if (isVerbose) {
			labels = verbose;
		} else {
			labels = not_verbose;
		}
		if ((needsBitMask & flagResearch) != 0) {
			checkResearch = setupCheckBox(labels[0], (apprBitMask & flagResearch) != 0);
			checkPanel.add(checkResearch);
		}
		if ((needsBitMask & flagPhotoEdit) != 0) {
			checkPhotoEdit = setupCheckBox(labels[1], (apprBitMask & flagPhotoEdit) != 0);
			checkPanel.add(checkPhotoEdit);
		}
		if ((needsBitMask & flagFootage) != 0) {
			checkFootage = setupCheckBox(labels[2], (apprBitMask & flagFootage) != 0);
			checkPanel.add(checkFootage);
		}
		if ((needsBitMask & flagLegal) != 0) {
			checkLegal = setupCheckBox(labels[3], (apprBitMask & flagLegal) != 0);
			checkPanel.add(checkLegal);
		}
		grabClosePanel = new JPanel();
		grabClosePanel.setLayout(newFlow);

		closeOrder = new JCheckBox("Closed");
		archiveOrder = new JCheckBox("Archived");
		grabClosePanel.add(closeOrder);
		grabClosePanel.add(archiveOrder);
		add(grabClosePanel);

		if (Users.currentUser.isAdmin || Users.currentUser.isArtist) {
			grabOrder = new JButton("Grab");
			grabOrder.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent arg0) {
					boolean itWorked = GFXSQL.grabOrder(Users.currentUser, model.grfxOrder, Users.currentUser.isAdmin);
				}

			});

			completeOrder = new JButton("Mark Complete");

			if (model.grfxOrder.getArtist() != null || model.grfxOrder.isComplete() || model.grfxOrder.isClosed() || model.grfxOrder.isArchived()) {
				grabOrder.setEnabled(false);
				completeOrder.setEnabled(false);
			} else {
				grabOrder.setEnabled(true);
				completeOrder.setEnabled(true);
			}

			grabClosePanel.add(grabOrder);
			grabClosePanel.add(completeOrder);
			
			closeOrder.setEnabled(false);
			archiveOrder.setEnabled(false);

			setBackgroundClosed();
			closeOrder.setSelected(model.isClosed());
			closeOrder.addItemListener(new ItemListener() {
				@Override
				public void itemStateChanged(ItemEvent e) {
					model.grfxOrder.setClosed((e.getStateChange() == ItemEvent.SELECTED));
					boolean itWorked = GFXSQL.closeOrder(model.getidOrder(), model.isClosed());
					setBackgroundClosed();
				}
			});

			setBackgroundArchived();
			archiveOrder.setSelected(model.isArchived());
			archiveOrder.addItemListener(new ItemListener() {
				@Override
				public void itemStateChanged(ItemEvent e) {
					model.grfxOrder.setArchived((e.getStateChange() == ItemEvent.SELECTED));
					GFXSQL.archiveOrder(model.getidOrder(), model.isArchived());
					setBackgroundArchived();
				}
			});

			if (Users.currentUser.isAdmin) {
				closeOrder.setEnabled(true);
				archiveOrder.setEnabled(true);
			}

		}
		setBackgroundsIfReady();
	}
	
	public void setBackgroundClosed(){
		if (model.isClosed()) {
			closeOrder.setBackground(Colors.getColor(Colors.CLOSED));
		} else {
			closeOrder.setBackground(Colors.getColor(Colors.DEFAULT_EVEN));
		}
	}
	
	public void setBackgroundArchived(){
		if (model.isArchived()) {
			archiveOrder.setBackground(Colors.getColor(Colors.ARCHIVED));
		} else {
			archiveOrder.setBackground(Colors.getColor(Colors.DEFAULT_EVEN));
		}
	}

	public void setBackgroundsIfReady() {
		boolean appr = isApprReady(ApprTreeNode.calcApprBitMask(model), ApprTreeNode.calcNeedsBitMask(model));

		this.setBackground((appr) ? Colors.getColor(Colors.READY) : Colors.getColor(Colors.NOT_READY));
		checkPanel.setBackground((appr) ? Colors.getColor(Colors.READY) : Colors.getColor(Colors.NOT_READY));
		grabClosePanel.setBackground((appr) ? Colors.getColor(Colors.READY) : Colors.getColor(Colors.NOT_READY));
		completeOrder.setEnabled(appr && !(model.grfxOrder.getArtist() != null || model.grfxOrder.isComplete() || model.grfxOrder.isClosed() || model.grfxOrder.isArchived()));
	}

	public boolean isApprReady(int apprBitMask, int needsBitMask) {
		if ((needsBitMask & flagResearch) != 0 && (apprBitMask & flagResearch) == 0) {
			return false;
		}
		if ((needsBitMask & flagPhotoEdit) != 0 && (apprBitMask & flagPhotoEdit) == 0) {
			return false;
		}
		if ((needsBitMask & flagFootage) != 0 && (apprBitMask & flagFootage) == 0) {
			return false;
		}
		if ((needsBitMask & flagLegal) != 0 && (apprBitMask & flagLegal) == 0) {
			return false;
		}
		return true;
	}

	private JCheckBox setupCheckBox(String label, boolean appr) {
		JCheckBox check = new JCheckBox(label);

		check.setSelected(appr);

		check.setBackground((appr) ? Colors.getColor(Colors.READY) : Colors.getColor(Colors.NOT_READY));

		check.addItemListener(new ItemListener() {

			@Override
			public void itemStateChanged(ItemEvent e) {
				JCheckBox source = (JCheckBox) e.getItemSelectable();
				source.setBackground((e.getStateChange() == ItemEvent.SELECTED) ? Colors.getColor(Colors.READY) : Colors.getColor(Colors.NOT_READY));
				setBackgroundsIfReady();
			}
		});

		return check;
	}

	public void addResearchListener(ItemListener itemListener) {
		if (checkResearch != null) {
			checkResearch.addItemListener(itemListener);
		}
	}

	public void addPhotoEditListener(ItemListener itemListener) {
		if (checkPhotoEdit != null) {
			checkPhotoEdit.addItemListener(itemListener);
		}
	}

	public void addFootageListener(ItemListener itemListener) {
		if (checkFootage != null) {
			checkFootage.addItemListener(itemListener);
		}
	}

	public void addLegalListener(ItemListener itemListener) {
		if (checkLegal != null) {
			checkLegal.addItemListener(itemListener);
		}
	}
*/
}
